package com.ruoyi.material.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;

import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.material.domain.vo.BaseLabelTypeTreeVo;
import com.ruoyi.material.domain.vo.LabelTypeVo;
import com.ruoyi.material.service.IBaseLabelTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.BaseLabel;
import com.ruoyi.material.service.IBaseLabelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标签类型Controller
 * 
 * @author ruoyi
 * @date 2021-07-23
 */
@RestController
@RequestMapping("/material/label")
public class BaseLabelController extends BaseController
{
    private Logger logger = LoggerFactory.getLogger(BaseFlieRecordController.class);
    @Autowired
    private IBaseLabelService baseLabelService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBaseLabelTypeService baseLabelTypeService;
    /**
     * 查询标签类型列表
     */
    @PreAuthorize("@ss.hasPermi('material:label:list')")
    @GetMapping("/list0")
    public TableDataInfo list0(LabelTypeVo labelTypeVo,String label)
    {
        startPage();
        BaseLabel baseLabel=new BaseLabel();
        System.out.println("label=="+label);
        baseLabel.setLabel(label);
        baseLabel.setTypeCode(labelTypeVo.getTypeCode());
        List<BaseLabel> list = baseLabelService.selectBaseLabelList(baseLabel);
        return getDataTable(list);
    }
    /**
     * 查询标签类型列表...用于标签管理之前的模块
     */
    @PreAuthorize("@ss.hasPermi('material:label:list')")
    @GetMapping("/list")
    public List<String> list(BaseLabel baseLabel)
    {
        startPage();
        List<BaseLabel> list = baseLabelService.selectBaseLabelList(baseLabel);
        Map<Integer,String> labelMap=new HashMap<>();
        list=baseLabelService.selectBaseLabelMap();
        List<String> labelList=new ArrayList<>();
        for (BaseLabel label:list) {
            labelList.add(label.getLabel());
        }
        return labelList;
    }

    /**
     * 导出标签类型列表
     */
    @PreAuthorize("@ss.hasPermi('material:label:export')")
    @Log(title = "标签类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseLabel baseLabel)
    {
        List<BaseLabel> list = baseLabelService.selectBaseLabelList(baseLabel);
        ExcelUtil<BaseLabel> util = new ExcelUtil<BaseLabel>(BaseLabel.class);
        return util.exportExcel(list, "标签类型数据");
    }

    /**
     * 获取标签类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:label:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseLabelService.selectBaseLabelById(id));
    }

    /**
     * 新增标签类型
     */
    @PreAuthorize("@ss.hasPermi('material:label:add')")
    @Log(title = "标签类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(String label, Integer typeCode)
    {
        BaseLabel baseLabel=new BaseLabel();
        baseLabel.setLabel(label);
        baseLabel.setTypeCode(typeCode.longValue());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        baseLabel.setCreateBy(loginUser != null ? loginUser.getUsername() : "anonymous");
        return toAjax(baseLabelService.insertBaseLabel(baseLabel));
    }

    /**
     * 修改标签类型
     */
    @PreAuthorize("@ss.hasPermi('material:label:edit')")
    @Log(title = "标签类型", businessType = BusinessType.UPDATE)
    @PostMapping("/updateLabel")
    public AjaxResult edit(Integer id, String label, String labelType)
    {
        System.out.println("label====="+label+"labelType---"+labelType);
        logger.info("BaseLabelController===edit===参数id="+id+"，参数标签label="+label+"，参数标签类型labelType="+labelType);
        Integer typeCode=baseLabelTypeService.selectBaseLabelTypeByLabel(labelType);
        BaseLabel baseLabel=new BaseLabel();
        baseLabel.setId(id.longValue());
        baseLabel.setLabel(label);
        baseLabel.setTypeCode(typeCode.longValue());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        baseLabel.setUpdateBy(loginUser != null ? loginUser.getUsername() : "anonymous");
        return toAjax(baseLabelService.updateBaseLabel(baseLabel));
    }

    /**
     * 删除标签类型
     */
    @PreAuthorize("@ss.hasPermi('material:label:remove')")
    @Log(title = "标签类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String updateBy=loginUser != null ? loginUser.getUsername() : "anonymous";
        return toAjax(baseLabelService.deleteBaseLabelByIds(ids,updateBy));
    }


    /**
     * 标签下拉树结构
     * @return
     */
    @GetMapping("/labelTreeSelect")
    public List<BaseLabelTypeTreeVo> showLabels(){
        List<BaseLabelTypeTreeVo> blttvList=new ArrayList<>();//第一级List
        blttvList= baseLabelService.buildLabelTreeSelect();
        return blttvList;
    }


    /**
     * 根据文件id找到标签
     * @return
     */
    @GetMapping("/showFileLabels/{id}")
    public String[] showFileLabels(@PathVariable("id")Integer id){
        return baseLabelService.showFileLabels(id);
    }
}
